package cn.iocoder.yudao.module.biz.controller.admin.htHiddentroubleflow;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble.vo.HiddentroubleJoinVO;
import cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble.vo.HiddentroublePageJoinVO;
import cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble.vo.HtHiddentroublePageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.htHiddentroubleflow.vo.*;
import cn.iocoder.yudao.module.biz.convert.htHiddentroubleflow.HtHiddentroubleflowConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentroubleflow.HtHiddentroubleflowDO;
import cn.iocoder.yudao.module.biz.service.htHiddentrouble.HtHiddentroubleService;
import cn.iocoder.yudao.module.biz.service.htHiddentroubleflow.HtHiddentroubleflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 隐患流程信息")
@RestController
@RequestMapping("/biz/ht-hiddentroubleRectify")
@Validated
public class HtHiddentroubleRectifyController {

    @Resource
    private HtHiddentroubleflowService htHiddentroubleflowService;

    @Resource
    private HtHiddentroubleService htHiddentroubleService;

    @PostMapping("/create")
    @ApiOperation("创建隐患流程信息")
    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:create')")
    public CommonResult<String> createHtHiddentroubleflow(@Valid @RequestBody HtHiddentroubleflowCreateReqVO createReqVO) {
        return success(htHiddentroubleflowService.createHtHiddentroubleflow(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新隐患流程信息")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:update')")
    public CommonResult<Boolean> updateHtHiddentroubleflow(@Valid @RequestBody HtHiddentroubleflowUpdateReqVO updateReqVO) {
        updateReqVO.setHtState("3");
        updateReqVO.setStep("2");
        htHiddentroubleflowService.updateHtHiddentroubleflow(updateReqVO);
        return success(true);
    }

    @PutMapping("/deferApply")
    @ApiOperation("更新隐患流程信息")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleDeferApply:update')")
    public CommonResult<Boolean> deferApplyHtHiddentroubleflow(@Valid @RequestBody HtHiddentroubleflowUpdateReqVO updateReqVO) {
        //登录用户ID
        Long userId = SecurityFrameworkUtils.getLoginUserId();

        updateReqVO.setDeferApplyPeoId(userId);
        updateReqVO.setHtState("5");//延期待审批
        updateReqVO.setDeferApplyDate(new Date());
        htHiddentroubleflowService.updateHtHiddentroubleflow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除隐患流程信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:delete')")
    public CommonResult<Boolean> deleteHtHiddentroubleflow(@RequestParam("id") String id) {
        htHiddentroubleflowService.deleteHtHiddentroubleflow(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得隐患流程信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:query')")
    public CommonResult<HiddentroubleflowJoinVO> getHtHiddentroubleflow(@RequestParam("id") String id) {
        HiddentroubleflowJoinVO hiddentroubleJoinVO = htHiddentroubleflowService.queryById(id,2);
        return success(hiddentroubleJoinVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得隐患流程信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:query')")
    public CommonResult<List<HtHiddentroubleflowRespVO>> getHtHiddentroubleflowList(@RequestParam("ids") Collection<String> ids) {
        List<HtHiddentroubleflowDO> list = htHiddentroubleflowService.getHtHiddentroubleflowList(ids);
        return success(HtHiddentroubleflowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得隐患流程信息分页")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:query')")
    public CommonResult<PageResult<HiddentroubleflowPageJoinVO>> getHtHiddentroubleflowPage(@Valid HtHiddentroubleflowPageReqVO pageVO) {
        PageResult<HiddentroubleflowPageJoinVO> result=new PageResult<>();
        int pageNo=pageVO.getPageNo();
        pageVO.setPageNo((pageNo-1)*pageVO.getPageSize());

        //登录用户ID
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        pageVO.setRectificationPeoId(userId);

        String[] states=pageVO.getStates();
        if(states.length!=0){
            if(states[0].equals("d")){
                //已整改：待验收、已闭合
                String[] s=new String[]{"3","4"};
                pageVO.setStates(s);
//                Long userId = SecurityFrameworkUtils.getLoginUserId();
//                pageVO.setRectificationPeoId(userId);
            }
        }else {
            //设置state 待整改、延期整改
            String[] s=new String[]{"1","2"};
            pageVO.setStates(s);
        }

        List<HiddentroubleflowPageJoinVO> list = htHiddentroubleflowService.selectHiddentroublePage(pageVO);
        Integer count= htHiddentroubleflowService.selectHiddentroubleCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return success(result);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出隐患流程信息 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentroubleRectify:export')")
    @OperateLog(type = EXPORT)
    public void exportHtHiddentroubleflowExcel(@Valid HtHiddentroubleflowExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HtHiddentroubleflowDO> list = htHiddentroubleflowService.getHtHiddentroubleflowList(exportReqVO);
        // 导出 Excel
        List<HtHiddentroubleflowExcelVO> datas = HtHiddentroubleflowConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "隐患流程信息.xls", "数据", HtHiddentroubleflowExcelVO.class, datas);
    }

}
